<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
          ①链表为什么要有虚拟头节点？如果没有虚拟头节点，那么删除首个头节点是不是操作不一样？必须单独列出来。
          但是如果有的话就一样的操作，最后返回dummy.next即可
          ②删除链表某个元素，只需要保存其next.next即可
          */
      function ListNode(val, next) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
      }
      var removeElements = function (head, val) {
        let dummy = new ListNode();
        dummy.next = head;
        let pre = dummy;
        let cur = pre.next;
        let next = null;
        while (cur) {
          if (cur.val == val) {
            pre.next = cur.next;
            cur = cur.next;
          } else {
            pre = cur;
            cur = cur.next;
          }
        }
        return dummy.next;
      };
    </script>
  </body>
</html>
